/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Models;

import Entity.Events;
import dbConnect.jdbcConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class adapterEvents {
    Connection con=null;
    public adapterEvents() throws Exception
    {
        con=jdbcConnection.getConnection();
    }
    public ArrayList<Events> getAllEvent() {
        try {
            ArrayList<Events> listEvents = new ArrayList<Events>();
            PreparedStatement pr = con.prepareStatement("select * from Events where stauts='true' order by codeEvent desc ");
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                Events event = new Events();
                event.setCodeEvent(rs.getInt(1));
                event.setNameEvent(rs.getString(2));
                event.setIntroduction(rs.getString(3));               
                event.setImages(rs.getString(4));
                event.setDate(rs.getString(5));
                listEvents.add(event);
            }
            return listEvents;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    //method search admin

    public ArrayList<Events> searchEvent(String name) {
        try {
            ArrayList<Events> listEvents = new ArrayList<Events>();
            PreparedStatement pr = con.prepareStatement("select*from Events where nameEvent like ?");
            pr.setString(1,"%" +name+"%");
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                Events event = new Events();
                event.setCodeEvent(rs.getInt(1));
                event.setNameEvent(rs.getString(2));
                event.setIntroduction(rs.getString(3));
                event.setImages(rs.getString(4));
                event.setDate(rs.getString(5));
                listEvents.add(event);
            }
            return listEvents;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    public ArrayList<Events> searchCodeEvent(int code) {
        try {
            ArrayList<Events> listEvents = new ArrayList<Events>();
            PreparedStatement pr = con.prepareStatement("select * from Events where codeEvent=?");
            pr.setInt(1, code);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                Events event = new Events();
                event.setCodeEvent(rs.getInt(1));
                event.setNameEvent(rs.getString(2));
                event.setIntroduction(rs.getString(3));
                event.setImages(rs.getString(4));
                event.setDate(rs.getString(5));
                listEvents.add(event);
            }
            return listEvents;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
//method insert admin

    public boolean insertEvent( String name, String content,String images,String date) {
        try {
            PreparedStatement pr = con.prepareStatement("insert into Events values(?,?,?,?)");
            pr.setString(1, name);
            pr.setString(2, content);
            pr.setString(3, images);
            pr.setString(4, date);
            int result = pr.executeUpdate();
            if (result > 0) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
    //method update admin

    public boolean updateEvent( String name, String content,String images,String date,int code) {
        try {
            PreparedStatement pr = con.prepareStatement("update Events set nameEvent=?,introduction=?,images=?,date=? where codeEvent=?");
            pr.setString(1, name);
            pr.setString(2, content);
            pr.setString(3,images);
            pr.setString(4, date);
            pr.setInt(5, code);
            int result = pr.executeUpdate();
            if (result > 0) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
    //method delete admin

    public boolean deleteEvent(int code) {
        try {
            PreparedStatement pr = con.prepareStatement("delete Events  where codeEvent=?");
            pr.setInt(1,code);
            int result = pr.executeUpdate();
            if (result > 0) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
    
}
